package com.example.demo.test;

import org.apache.commons.text.StringEscapeUtils;

public class htmlutil {


    public static void main(String[] args) {
        System.out.println("转义/反转义Java字符串");
        String javaString = "这是Java字符串";
        System.out.println(StringEscapeUtils.escapeJava(javaString));
        System.out.println(StringEscapeUtils.unescapeJava(StringEscapeUtils.escapeJava("\\u8FD9\\u662FJava\\u5B57\\u7B26\\u4E32")));
        System.out.println("-------------------------------------------------------------");
        System.out.println("转义/反转义Json字符串");
        String jsonString = "{\"keyword\": \"这是Json字符串\"}";
        System.out.println(StringEscapeUtils.escapeJson(jsonString));
        System.out.println(StringEscapeUtils.unescapeJson(StringEscapeUtils.escapeJson(jsonString)));
        System.out.println("-------------------------------------------------------------");
        //除了html4还有html3等格式
        System.out.println("转义/反转义Html字符串");
        String htmlString = "<strong>加粗字符</strong>";
        System.out.println(StringEscapeUtils.escapeHtml4(htmlString));
        System.out.println(StringEscapeUtils.unescapeHtml4(StringEscapeUtils.escapeHtml4(htmlString)));
        System.out.println("-------------------------------------------------------------");
        System.out.println("-------------------------------------------------------------");
        //除了html4还有html3等格式
        System.out.println("转义/反转义Html字符串");
        String htmlString1 = "<h1>标题h1</h1>\n" +
                "<h5>标题h5</h5>\n" +
                "<p><strong>加粗</strong><br />\n" +
                "<em>斜体</em><br />\n" +
                "<del>删除线</del><br />\n" +
                "<a href=\"https://github.com/vbenjs/vue-vben-admin\">链接</a><br />\n" +
                "↓分割线↓</p>\n" +
                "<hr />\n" +
                "<ul>\n" +
                "<li>无序列表1\n" +
                "<ul>\n" +
                "<li>无序列表1.1</li>\n" +
                "</ul>\n" +
                "</li>\n" +
                "</ul>\n" +
                "<ol>\n" +
                "<li>有序列表1</li>\n" +
                "<li>有序列表2</li>\n" +
                "</ol>\n" +
                "<ul>\n" +
                "<li class=\"vditor-task\"><input disabled=\"\" type=\"checkbox\" /> 任务列表1</li>\n" +
                "<li class=\"vditor-task vditor-task--done\"><input checked=\"\" disabled=\"\" type=\"checkbox\" /> 任务列表2</li>\n" +
                "</ul>\n" +
                "<blockquote>\n" +
                "<p>引用示例</p>\n" +
                "</blockquote>\n" +
                "<pre><code class=\"language-js\">// 代码块:\n" +
                "(() =&gt; {\n" +
                "  var htmlRoot = document.getElementById('htmlRoot');\n" +
                "  var theme = window.localStorage.getItem('__APP__DARK__MODE__');\n" +
                "  if (htmlRoot &amp;&amp; theme) {\n" +
                "    htmlRoot.setAttribute('data-theme', theme);\n" +
                "    theme = htmlRoot = null;\n" +
                "  }\n" +
                "})();\n" +
                "</code></pre>\n" +
                "<table>\n" +
                "<thead>\n" +
                "<tr>\n" +
                "<th>表格</th>\n" +
                "<th>示例</th>\n" +
                "<th>\uD83C\uDF89️</th>\n" +
                "</tr>\n" +
                "</thead>\n" +
                "<tbody>\n" +
                "<tr>\n" +
                "<td>1</td>\n" +
                "<td>2</td>\n" +
                "<td>3</td>\n" +
                "</tr>\n" +
                "<tr>\n" +
                "<td>4</td>\n" +
                "<td>5</td>\n" +
                "<td>6</td>\n" +
                "</tr>\n" +
                "</tbody>\n" +
                "</table>";
        System.out.println(StringEscapeUtils.escapeHtml4(htmlString1));
        System.out.println(StringEscapeUtils.unescapeHtml4(StringEscapeUtils.escapeHtml4(htmlString1)));
        System.out.println("-------------------------------------------------------------");
        //除了xml10还有xml11等格式
        System.out.println("转义/反转义xml字符串");
        String xmlString = "<xml>\"xml字符串\"</xml>";
        System.out.println(StringEscapeUtils.escapeXml10(xmlString));
        System.out.println(StringEscapeUtils.unescapeXml(StringEscapeUtils.escapeXml10(xmlString)));
        System.out.println("-------------------------------------------------------------");
        System.out.println("转义/反转义csv字符串");
        String csvString = "1997,Ford,E350,\"Super, luxurious truck\"";
        System.out.println(StringEscapeUtils.escapeCsv(csvString));
        System.out.println(StringEscapeUtils.unescapeCsv(StringEscapeUtils.escapeCsv(csvString)));
        System.out.println("-------------------------------------------------------------");
        System.out.println("转义/反转义Java Script字符串");
        String jsString = "<script>alert('1111')</script>";
        System.out.println(StringEscapeUtils.escapeEcmaScript(jsString));
        System.out.println(StringEscapeUtils.unescapeEcmaScript(StringEscapeUtils.escapeEcmaScript(jsString)));

        System.out.println("-------------------------------------------------------------");
        System.out.println("转义/反转义Java Script字符串");
        String jsString1 = "<h1>标题h1</h1>\\n\" +\n" +
                "                \"<h5>标题h5</h5>\\n\" +\n" +
                "                \"<p><strong>加粗</strong><br />\\n\" +\n" +
                "                \"<em>斜体</em><br />\\n\" +\n" +
                "                \"<del>删除线</del><br />\\n\" +\n" +
                "                \"<a href=\\\"https://github.com/vbenjs/vue-vben-admin\\\">链接</a><br />\\n\" +\n" +
                "                \"↓分割线↓</p>\\n\" +\n" +
                "                \"<hr />\\n\" +\n" +
                "                \"<ul>\\n\" +\n" +
                "                \"<li>无序列表1\\n\" +\n" +
                "                \"<ul>\\n\" +\n" +
                "                \"<li>无序列表1.1</li>\\n\" +\n" +
                "                \"</ul>\\n\" +\n" +
                "                \"</li>\\n\" +\n" +
                "                \"</ul>\\n\" +\n" +
                "                \"<ol>\\n\" +\n" +
                "                \"<li>有序列表1</li>\\n\" +\n" +
                "                \"<li>有序列表2</li>\\n\" +\n" +
                "                \"</ol>\\n\" +\n" +
                "                \"<ul>\\n\" +\n" +
                "                \"<li class=\\\"vditor-task\\\"><input disabled=\\\"\\\" type=\\\"checkbox\\\" /> 任务列表1</li>\\n\" +\n" +
                "                \"<li class=\\\"vditor-task vditor-task--done\\\"><input checked=\\\"\\\" disabled=\\\"\\\" type=\\\"checkbox\\\" /> 任务列表2</li>\\n\" +\n" +
                "                \"</ul>\\n\" +\n" +
                "                \"<blockquote>\\n\" +\n" +
                "                \"<p>引用示例</p>\\n\" +\n" +
                "                \"</blockquote>\\n\" +\n" +
                "                \"<pre><code class=\\\"language-js\\\">// 代码块:\\n\" +\n" +
                "                \"(() =&gt; {\\n\" +\n" +
                "                \"  var htmlRoot = document.getElementById('htmlRoot');\\n\" +\n" +
                "                \"  var theme = window.localStorage.getItem('__APP__DARK__MODE__');\\n\" +\n" +
                "                \"  if (htmlRoot &amp;&amp; theme) {\\n\" +\n" +
                "                \"    htmlRoot.setAttribute('data-theme', theme);\\n\" +\n" +
                "                \"    theme = htmlRoot = null;\\n\" +\n" +
                "                \"  }\\n\" +\n" +
                "                \"})();\\n\" +\n" +
                "                \"</code></pre>\\n\" +\n" +
                "                \"<table>\\n\" +\n" +
                "                \"<thead>\\n\" +\n" +
                "                \"<tr>\\n\" +\n" +
                "                \"<th>表格</th>\\n\" +\n" +
                "                \"<th>示例</th>\\n\" +\n" +
                "                \"<th>\\uD83C\\uDF89️</th>\\n\" +\n" +
                "                \"</tr>\\n\" +\n" +
                "                \"</thead>\\n\" +\n" +
                "                \"<tbody>\\n\" +\n" +
                "                \"<tr>\\n\" +\n" +
                "                \"<td>1</td>\\n\" +\n" +
                "                \"<td>2</td>\\n\" +\n" +
                "                \"<td>3</td>\\n\" +\n" +
                "                \"</tr>\\n\" +\n" +
                "                \"<tr>\\n\" +\n" +
                "                \"<td>4</td>\\n\" +\n" +
                "                \"<td>5</td>\\n\" +\n" +
                "                \"<td>6</td>\\n\" +\n" +
                "                \"</tr>\\n\" +\n" +
                "                \"</tbody>\\n\" +\n" +
                "                \"</table>";

        System.out.println(StringEscapeUtils.escapeEcmaScript(jsString1));
        System.out.println("-------------------------------------------------------------");
        //System.out.println(StringEscapeUtils.unescapeEcmaScript(StringEscapeUtils.escapeEcmaScript(jsString1)));

    }
}
